import collections
from django.http import JsonResponse
from django.shortcuts import render
from web import models
from django.db.models import Count


def statistics(request, project_id):
    return render(request, "statistics.html")


def statistics_priority(request, project_id):
    """ 获取饼状图数据,按照优先级生成饼图
        [{
                name:xx,
                y:xx
            },{
                name:xx,
                y:xx
        }]

    """
    start = request.GET.get("start")
    end = request.GET.get("end")
    # 找到所有的额问题，根据优先级分组，每个优先级的问题数量
    # 1. 构造字典
    data_dict = collections.OrderedDict()
    for key, text in models.Issues.priority_choices:
        data_dict[key] = {"name": text, "y": 0}
    # 2. 取数据库查询所有分组得到的数据量
    result = models.Issues.objects.filter(project_id=project_id, create_datetime__gte=start,
                                          create_datetime__lt=end).values("priority").annotate(ct=Count("id"))

    # 3. 把分组得到的数据更新到data_dict中
    for item in result:
        data_dict[item["priority"]]["y"] = item["ct"]
    print(data_dict.values())
    return JsonResponse({"status": True, "data": list(data_dict.values())})


def statistics_project_user(request, project_id):
    """ 项目成员每个人被分配的任务数量（问题类型的配比）"""
    """
    构造的数据结构
    all_user_dict={
        1(user_id):{
            "name" : "王洋"，
            "status":{
                1:0,
                2:0,
                3:0,
                4:0,
                5:0,
                6:0,
                7:0
            }
        },
    }
    
    """
    start = request.GET.get("start")
    end = request.GET.get("end")
    # 1. 所有项目成员 及 未指派
    # 1.1 项目的创建者加入字典
    all_user_dict = collections.OrderedDict()
    all_user_dict[request.tracer.project.creator.id] = {
        "name": request.tracer.project.creator.username,
        "status": {item[0]: 0 for item in models.Issues.status_choices}
    }
    all_user_dict["None"] = {
        "name": "未指派",
        "status": {item[0]: 0 for item in models.Issues.status_choices}
    }
    # 1.2 项目的参与者加入字典

    user_list = models.ProjectUser.objects.filter(project_id=project_id)
    for item in user_list:
        all_user_dict[item.user_id] = {
            "name": item.user.username,
            "status": {item[0]: 0 for item in models.Issues.status_choices}
        }

    # 2. 去数据库获取相关的所有问题
    issues = models.Issues.objects.filter(project_id=project_id, create_datetime__gte=start,
                                          create_datetime__lt=end)
    # 2.1 循环所有的问题 ，看是否是被指派的
    for item in issues:
        if not item.assign:  # 未指派
            all_user_dict["None"]["status"][item.status] += 1
        else:  # 指派的人的id
            all_user_dict[item.id]["status"][item.status] += 1

    # 3. 获取所有的成员
    categories = [data["name"] for data in all_user_dict.values()]
    # 4. 再构造一个字典
    """
    data_result_dict = {
        1:{
            "name":"新建",
            "data":[]
        },
        2:{
            "name":"已完成",
            "data":[]
        },
        
    }
    
    """
    data_result_dict = collections.OrderedDict()
    for item in models.Issues.status_choices:
        data_result_dict[item[0]] = {"name": item[1], "data": []}

    for key, text in models.Issues.status_choices:
        for row in all_user_dict.values():
            count = row['status'][key]  # 拿到数量
            data_result_dict[key]["data"].append(count)

    context = {
        "status": True,
        "data": {
            "categories": categories,
            "series": list(data_result_dict.values())
        }
    }
    return JsonResponse(context)


